<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="证书业务id" prop="certificateId">
        <el-input v-model="form.certificateId" placeholder="请输入证书业务id"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="证书名称" prop="certificateName">
        <el-input v-model="form.certificateName" placeholder="请输入证书名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="员工工号" prop="usercode">
        <el-input v-model="form.usercode" placeholder="请输入员工工号"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入员工姓名"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="岗位id" prop="postId">
        <el-input v-model="form.postId" placeholder="请输入岗位id"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="岗位名称" prop="postName">
        <el-input v-model="form.postName" placeholder="请输入岗位名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="文件id" prop="fileUid">
        <el-input v-model="form.fileUid" placeholder="请输入文件id"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="证书模板id" prop="certificateTemplateId">
        <el-input v-model="form.certificateTemplateId" placeholder="请输入证书模板id"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="证书类型" prop="certificateType">
        <el-input v-model="form.certificateType" placeholder="请输入证书类型"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="证书性质" prop="certificateNature">
        <el-input v-model="form.certificateNature" placeholder="请输入证书性质"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="颁发机构" prop="authority">
        <el-input v-model="form.authority" placeholder="请输入颁发机构"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="开始日期" prop="startDate">
        <el-input v-model="form.startDate" placeholder="请输入开始日期"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="结束日期" prop="endDate">
        <el-input v-model="form.endDate" placeholder="请输入结束日期"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="审核状态" prop="auditStatus">
        <el-input v-model="form.auditStatus" placeholder="请输入审核状态"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="发证时间" prop="issuanceDate">
        <el-input v-model="form.issuanceDate" placeholder="请输入发证时间"/>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="ZsglCertificateQualificationsDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/train/zsglCertificateQualifications'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { certification_authority,certificate_nature,certificate_type } = useDict('certification_authority','certificate_nature','certificate_type')

// 提交表单数据
const form = reactive({
		id:'',
	  certificateId: '',
	  certificateName: '',
	  usercode: '',
	  username: '',
	  postId: '',
	  postName: '',
	  fileUid: '',
	  certificateTemplateId: '',
	  certificateType: '',
	  certificateNature: '',
	  authority: '',
	  startDate: '',
	  endDate: '',
	  auditStatus: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取zsglCertificateQualifications信息
  if (id) {
    form.id = id
    getzsglCertificateQualificationsData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getzsglCertificateQualificationsData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>